package com.ctsi.ssdc.common;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: upd6
 * @description: 返回类
 * @author: yangsc
 * @create: 2021-08-14 11:06
 **/
@Data
@EqualsAndHashCode(callSuper = false)
public class R implements Serializable {

    private static final long serialVersionUID = -4021869530135228283L;

    private Map<String, Object> data;

    private String message;

    private int code;

    private boolean success;

    public static R ok() {
        R r = new R();
        r.setCode(200);
        r.setSuccess(true);
        r.setMessage("操作成功");
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.setSuccess(true);
        r.setCode(200);
        r.setMessage(msg);
        return r;
    }

    public R data(String key, Object value) {
        data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> data) {
        this.data = data;
        return this;
    }


    public static R err(String message) {
        R r = new R();
        r.setCode(500);
        r.setSuccess(false);
        r.setMessage(message);
        return r;
    }

    public static R miss(String message) {
        R r = new R();
        r.setCode(400);
        r.setSuccess(false);
        r.setMessage(message);
        return r;
    }


    public R() {
        data = new HashMap<>();
    }
}
